package com.uniledger.exchange.dapp;


import android.os.Build;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.uniledger.commonlib.lazy.LazyFragmentPagerAdapter;
import com.uniledger.commonlib.web3.OnGetBalanceListener;
import com.uniledger.commonlib.web3.OnSignMessageListener;
import com.uniledger.commonlib.web3.OnSignPersonalMessageListener;
import com.uniledger.commonlib.web3.OnSignTransactionListener;
import com.uniledger.commonlib.web3.OnSignTypedMessageListener;
import com.uniledger.commonlib.web3.OnVerifyListener;
import com.uniledger.commonlib.web3.Web3View;
import com.uniledger.commonlib.web3.entity.Message;
import com.uniledger.commonlib.web3.entity.TypedData;
import com.uniledger.commonlib.web3.entity.URLLoadInterface;
import com.uniledger.commonlib.web3.entity.URLLoadReceiver;
import com.uniledger.commonlib.web3.entity.Web3Transaction;
import com.uniledger.commonlib.web3.util.C;
import com.uniledger.commonlib.web3.util.Web3Utils;
import com.uniledger.exchange.BuildConfig;
import com.uniledger.exchange.R;
import com.uniledger.exchange.base.fragment.BaseCoreFragment;

import butterknife.BindView;

public class DappFragment extends BaseCoreFragment implements
        LazyFragmentPagerAdapter.Laziable,
        OnSignTransactionListener,
        OnSignPersonalMessageListener,
        OnSignTypedMessageListener,
        OnSignMessageListener,
        OnVerifyListener,
        OnGetBalanceListener,
        URLLoadInterface {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;


    public DappFragment() {
        // Required empty public constructor
    }

    private URLLoadReceiver URLReceiver;

    @BindView(R.id.web3View)
    Web3View web3;

    public static DappFragment newInstance(String param1, String param2) {
        DappFragment fragment = new DappFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected void parseArguments(Bundle bundle) {

    }

    @Override
    protected void initData() {
        setupWeb3();
        //Url广播接收者
        URLReceiver = new URLLoadReceiver(getActivity(), this);
    }

    @Override
    public int getViewResourceId() {
        return R.layout.fragment_dapp;
    }

    @Override
    protected void initView() {

    }

    @Override
    public void onGetBalance(String balance) {

    }

    @Override
    public void onSignMessage(Message<String> message) {

    }

    @Override
    public void onSignPersonalMessage(Message<String> message) {

    }

    @Override
    public void onSignTransaction(Web3Transaction transaction, String url) {

    }

    @Override
    public void onSignTypedMessage(Message<TypedData[]> message) {

    }

    @Override
    public void onVerify(String message, String signHex) {

    }

    @Override
    public void onWebpageLoaded(String url) {

    }

    private void setupWeb3() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG);
        }

        web3.setChainId(1);
        web3.setRpcUrl(C.ETH_RPC_URL);

        //TODO 设置钱包地址
//        web3.setWalletAddress(new Address(wallet.address));

        web3.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView webview, int newProgress) {
                //TODO 改变进度条
//                if (newProgress == 100) {
//                    progressBar.setVisibility(View.GONE);
//                    swipeRefreshLayout.setRefreshing(false);
//                } else {
//                    progressBar.setVisibility(View.VISIBLE);
//                    progressBar.setProgress(newProgress);
//                    swipeRefreshLayout.setRefreshing(true);
//                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });

        web3.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                urlTv.setText(url);
                return false;
            }
        });

        web3.setOnSignMessageListener(this);
        web3.setOnSignPersonalMessageListener(this);
        web3.setOnSignTransactionListener(this);
        web3.setOnSignTypedMessageListener(this);
        web3.setOnVerifyListener(this);
        web3.setOnGetBalanceListener(this);
    }

    /**
     * 加载URL
     *
     * @param urlText
     * @return
     */
    private boolean loadUrl(String urlText) {
        web3.loadUrl(Web3Utils.formatUrl(urlText));
        web3.requestFocus();
//        viewModel.setLastUrl(getContext(), urlText);
//        adapter.add(urlText);
//        adapter.notifyDataSetChanged();
//        dismissKeyboard();
//        getActivity().sendBroadcast(new Intent(RESET_TOOLBAR));
        return true;
    }
}
